package com.example.mybluetooth;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.UUID;


public class BlueSocketTest extends AppCompatActivity {
    private TextView inputinfo, sendinfo;
    private EditText inputtext;
    private Button send;
    private BluetoothAdapter mBlueToothAdapter;
    private BluetoothDevice mDevice;
    private static OutputStream outputStream;
    private String name, address;
    private InputStream inputStream;
    private BluetoothSocket mBluetoothSocket;
    private final UUID mUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");//蓝牙串口服务的UUID

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blue_socket_test);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        address = intent.getStringExtra("address");
        Log.i("test", "onCreate: " + name + address);
        inview();
        initIO();
        inintBlueDevice();
    }




    private void inview() {
        inputinfo = findViewById(R.id.inputinfo);
        sendinfo = findViewById(R.id.sendinfo);
        inputtext = findViewById(R.id.inputtext);
        send = findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = inputtext.getText().toString();
                send(s);
            }
        });
    }

    private void CloseIO() {
        try {
            inputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void initIO() {
        mBlueToothAdapter = BluetoothAdapter.getDefaultAdapter();//初始化蓝牙适配器
        mDevice = mBlueToothAdapter.getRemoteDevice(address);//拿到要连接的蓝牙设备
        try {
            mBluetoothSocket = mDevice.createRfcommSocketToServiceRecord(mUUID);
            mBluetoothSocket.connect();
            inputStream =mBluetoothSocket.getInputStream();
            outputStream = mBluetoothSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void inintBlueDevice() {
        if (mBluetoothSocket.isConnected()){
            Toast.makeText(this, "连接成功", Toast.LENGTH_SHORT).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        byte [] bytes = new byte[1024];
                        while (true){
                            int read = inputStream.read(bytes);
                            Log.i("TAG", "run: "+new String(bytes,0,read));
                            String a = new String(bytes,0,read,"GBK");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    inputinfo.setText(a);
                                }
                            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else {
            Toast.makeText(this, "蓝牙连接出错", Toast.LENGTH_SHORT).show();
        }
    }


    private void send(String content) {
        try {
            if (mBluetoothSocket.isConnected()){
                outputStream.write(content.getBytes("GBK"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //释放资源
    @Override
    protected void onDestroy() {
        super.onDestroy();
        CloseIO();
        try {
            if (mBluetoothSocket.isConnected()) {
                //关闭socket
                mBluetoothSocket.close();
                mBlueToothAdapter = null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
